<?php
/**
* @filename source_module.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/

class ctrl_course_collection extends course_collection{
    public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
    
    static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
    /*
	*	取出一条信息
	*/
	public function do_detail($cc_id='') {
		$detail = array();
        $cc_id = isset($_REQUEST['cc_id']) ? $_REQUEST['cc_id'] : '';
		$detail = $this->one_info('course_collection','cc_id',$cc_id);
        //DEBUG 调试输出
        include template('course/collection/detail');
	}
	
	/*
	 *	取出收藏列表
	 */
	public function do_detail_list() {	
		global $_G;
		$detail = array();
		$page = max(1, intval($_GET['page']));
		$perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : '10';
		$start=(($page-1) * $perpage);
		$wheresql = "";
		$wheresql .= " AND cc.isdelete = 0  and cc.user_id = ".$_G['user_id'];
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
				
		$orderby = " ORDER BY cc.create_dateline DESC,cc.cc_id DESC ";	
		//DEBUG 初始化请求请求获取一页列表数据的参数
		$page_condition=array(
				'page' => $page,//int 请求页面 页码
				'limit' => $limit,//int 每页请求个数
				'perpage' => $perpage,//int 每页显示个数
				'wheresql' => $wheresql,//string //条件SQL语句
				'orderby' => $orderby,//string 排序规则
		);		
		$page_result = $this->detail_list('course_collection',$page_condition);	
	
		//DEBUG 调试输出
		if($page_result){
			$return['code'] = '0';
			$return['data']=$page_result;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
				
	}
    
	/*
	*  添加课程收藏
	*/
	public function do_add() {
	global $_G;	
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$course_collection['cm_id']=isset($_REQUEST['cm_id']) ? $_REQUEST['cm_id']:'';
						
		$course_collection['create_dateline'] = TIMESTAMP;
		$course_collection['modify_dateline'] = TIMESTAMP;
		$course_collection['user_id'] = $_G['user_id'];
				
		$insert_data = $this->add('course_collection',$course_collection);		
		if($insert_data){										
			$return['code'] = '0';
			$return['data']=$insert_data;		
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	}
    
	/*
	*  编辑系统信息
	*/
	public function do_edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$course_collection = isset($_REQUEST['course_collection']) && !empty($_REQUEST['course_collection']) ? $_REQUEST['course_collection']:'';
        $cc_id = isset($_REQUEST['cc_id']) ? $_REQUEST['cc_id'] : '';
        if($cc_id){
            if('1'==$issubmit){
                $course_collection['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach($course_collection AS $key => $value){
                    if(is_array($value) && !empty($value)){
                        $course_collection[$key] = implode(",", $value);
                    }
                }                
                $where = array('cc_id'=>$cc_id);
                $effect_row = $this->edit('course_collection',$course_collection,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=course&action=collection&do=index');
                }else{
                    showmessage('操作失败','index.php?mod=course&action=collection&do=index');
                }
            }else{
                //DEBUG 获取操作对象信息
                $info = $this->one_info('course_collection','cc_id',$cc_id);
                include template('course/collection/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=course&action=collection&do=index');
        }
	}
    

	/*
	 *  删除收藏
	 */
	public function do_delete() {
		global $_G;
		$cc_id = isset($_REQUEST['cc_id']) ? $_REQUEST['cc_id'] : '';
		if($cc_id){
			$effect_row = $this->delete("course_collection", array("cc_id" => $cc_id));
			if($effect_row){
				$return['code'] = '0';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '3';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	}
	/*
	*  系统信息列表
	*/
	public function do_index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        if($_G['user_id']){
            $wheresql = " AND user_id='".$_G['user_id']."' ";
            $orderby = " ORDER BY cc_id DESC ";
            //DEBUG 初始化请求请求获取一页列表数据的参数
            $page_condition=array(
                'page' => $page,//int 请求页面 页码
                'limit' => $limit,//int 每页请求个数
                'perpage' => $perpage,//int 每页显示个数
                'wheresql' => $wheresql,//string //条件SQL语句 
                'orderby' => $orderby,//string 排序规则
            );
            //DEBUG 列表数据返回结构
            /*
            $page_result = array(
                //int 返回结果总数
                'total_rows' => $total_rows,
                //array 一页数据数组
                'page_data' => $page_data 
            );
            */
            $page_result = $this->index('course_collection',$page_condition);
            foreach($page_result['page_data'] AS $key => $value){
                $tmp_value = $this->get_field_value_by_id('course_main','cm_id',$value['cm_id'],'cm_id,cm_name,cm_brief,cm_img,cm_price,cm_count,so_id');
                $so_name = $this->get_field_value_by_id('school_online','so_id',$tmp_value['so_id'],'so_name');
                $tmp_value['so_name']=$so_name['so_name'];
                $tmp_value['cm_img']=$tmp_value['cm_img'];
                $page_result['page_data'][$key]=$tmp_value;
            }
        }
        if($_G['gp_api']){
            $return['code'] = '0';
            $return['data']['course_collection_index']=$page_result;
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=course&action=collection&do=index&keyword=".$keyword);
            include template('course/collection/index');	  
        }
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function do_api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $orderby = " ORDER BY cc_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->api('course_collection',$page_condition);      
        if('json'==$return_type){
            $return = json_encode($page_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
		echo $return;
	}
}
?>